package com.wowangz.cms.systemmanage.exportreport.lucentask;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;

import org.apache.commons.jexl2.Main;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

public class FTPHelper {

  public FTPClient getFtpClient(){
        return ftpClient;
    }
	
  private  Log log = LogFactory.getLog(getClass()) ;
	
	private String userName; 
	private String password;
	private String ip; 
	private int port; 
	
	private FTPClient ftpClient = null; 
	
	public FTPHelper(){
		this("lixin", "elena.li!2016", "114.112.56.134", 21);
	}
	//构造方法初始化类
	public FTPHelper(String userName, String password, String ip, int port) {
		this.userName = userName;
		this.password = password;
		this.ip = ip;
		this.port = port;
	}
      //连接ftp
	public boolean connectServer() throws Exception{
		boolean flag = true;
		if (ftpClient == null) { 
			ftpClient = new FTPClient();
			ftpClient.connect(ip,port);
			
			int reply = ftpClient.getReplyCode(); 
			if (!FTPReply.isPositiveCompletion(reply)) {
				ftpClient.disconnect();
				log.warn("FTP server refused connection.");
				return false ;
			}
			
			boolean bok = ftpClient.login(userName, password);
			if (!bok)  {
				try {
					ftpClient.disconnect() ;
					ftpClient = null ;
				} catch (Exception e) { }
				throw new Exception("can not login ftp server") ;
			}
			
			ftpClient.setBufferSize(1024);
			ftpClient.setControlEncoding("GBK"); 
			ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 
			ftpClient.setDataTimeout(120000); 
			ftpClient.enterLocalPassiveMode();
			ftpClient.setUseEPSVwithIPv4(false);
		} 
		return flag;
	}
    
	public void closeConnect() { 
		try {
			if (ftpClient != null) {
				ftpClient.logout();
				ftpClient.disconnect();
			}
		} catch (Exception e) {
		}

	}
      //下载文件
	public boolean downloadFile(String remotePath, String fileName,  BufferedOutputStream os) throws Exception {
		
		FileOutputStream fos = null ; 
		try {
		   
			ftpClient.enterLocalPassiveMode(); 
			ftpClient.changeWorkingDirectory(remotePath) ;
			boolean bok = ftpClient.retrieveFile(fileName, os);
			os.close() ;
			return bok ;
			
		} catch (Exception e) {
			throw e ;
		}
		finally {
			if (fos!=null) {
				try {
					os.close() ;
					os = null ;
				} catch (Exception e2) { }
			}
		} 
	}
	
	public static void main(String[] args) throws Exception {
		 FTPHelper fh = new FTPHelper();
	        
		 FileOutputStream fo = new FileOutputStream(new File("c:/test.mp4"));
	        if(fh.connectServer()){
	        	
	        	FTPClient ftpClient = fh.getFtpClient();
	        	ftpClient.enterLocalPassiveMode(); 
				ftpClient.changeWorkingDirectory("/20161201/ed9880fbc356e6391a88a69ccd71909d_073015") ;
                ftpClient.retrieveFile("ed9880fbc356e6391a88a69ccd71909d.mp4", fo);
                fo.flush();
                fo.close();
               fh.closeConnect();
	        }
	}
}
